import sublime
import sublime_plugin
import os
import subprocess
import sys
BASE_PATH = os.path.abspath(os.path.dirname(__file__))
PACKAGE_NAME = 'SyncFile'
PACKAGE_SETTINGS = PACKAGE_NAME + '.sublime-settings'
print(PACKAGE_SETTINGS)

class SyncFileCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		self.view.run_command('save')
		# 获取配置文件
		settings = sublime.load_settings(PACKAGE_SETTINGS)
		location = settings.get('location')
		rules = settings.get(location)
		for item in rules:
			self.handleFile(item)

	def handleFile(self,item):
		local = item['local']
		remote = item['remote']
		filename = self.view.file_name()
		print(filename)
		print(local)
		if local in filename:
			local_len = len(local)
			change_file = filename[local_len:]
			true_remote_file = remote + change_file
			cmd = r"cd /Users/dxm/Library/Application\ Support/Sublime\ Text\ 3/Packages/User/ && ./cp.exp " + filename + " " + true_remote_file
			res = os.popen(cmd)
			print(res.read())
		else:
			print("nothin to do ..")





